<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <!-- <form action="" enctype="application/x-www-form-urlencoded"></form> -->
    <h1>我是产品页</h1>
    <button class="btn1">点击我返送get请求</button>
    <button class="btn2">点击我发送post请求</button>
    <script>
        let btn1  = document.querySelector(".btn1");
        btn1.onclick = function(){
            let xhr = new XMLHttpRequest();
            xhr.open("get","/getdata?name=zhangsan&age=20",true);
            // xhr.onreadystatechange = function(){
            //     if(xhr.status==200 && xhr.readyState==4){
            //         console.log(xhr.responseText);
            //     }
            // }
            xhr.onload = function(){
                console.log(xhr.readyState , xhr.status);
                // console.log(xhr.responseText);
                let res = JSON.parse(xhr.responseText);
                console.log(res);
            }
            xhr.send();
        }

        let btn2 = document.querySelector(".btn2");
        btn2.onclick = function(){
            let xhr = new XMLHttpRequest();
            xhr.open("post","/getpostdata",true);
            xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
            xhr.onload = function(){
                console.log(xhr.readyState , xhr.status);
                // console.log(xhr.responseText);
                let res = JSON.parse(xhr.responseText);
                console.log(res);
            }
            let str = "name=zhangsan&age=20";
            xhr.send(str);
        }

    </script>
</body>
</html>